//
//  YongQingUsePapersVC.swift
//  YongQingFont
//
//  Created by 李东 on 2024/4/20.
//

import UIKit
import SnapKit

class YongQingUsePapersVC: YongQingBaseVC {
    
    lazy var emptyLabel = UILabel()
    lazy var whiteView = UIView()
    lazy var todayUseLabel = UILabel()
    lazy var limitLabel = UILabel()
    lazy var userBtn = UIButton(type: .custom)
    lazy var limitCount = 0
    lazy var userCount = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "节约用纸"
        initUI()
        reloadData()
    }
    
    @objc func showSelectPciker() {
        let vc = YongQingConfigVC()
        vc.modalPresentationStyle = .overFullScreen
        vc.selectCallBack = {[weak self] count in
            guard let self = self else { return }
            self.limitCount = count
            let list = ["\(self.limitCount)", "\(self.userCount)"]
            UserDefaults.standard.set(list, forKey: self.foramtter.string(from: Date()))
            UserDefaults.standard.synchronize()
            self.reloadData()
        }
        present(vc, animated: true)
    }
    
    func initUI() {
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_add")?.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(showSelectPciker))
        
        emptyLabel.textColor = .black
        emptyLabel.font = UIFont.systemFont(ofSize: 16)
        emptyLabel.text = "还未添加用纸记录\n点击 [+] 做好你的用纸规划"
        emptyLabel.textAlignment = .center
        emptyLabel.numberOfLines = 0
        view.addSubview(emptyLabel)
        emptyLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-50)
        }
        
        whiteView.backgroundColor = .white
        whiteView.layer.cornerRadius = 15
        whiteView.layer.masksToBounds = true
        view.addSubview(whiteView)
        whiteView.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(20)
            make.height.equalTo(284)
        }
        
        let topLabel = UILabel()
        topLabel.textColor = .black
        topLabel.text = "今日用纸限制（张）"
        topLabel.font = UIFont.systemFont(ofSize: 16)
        whiteView.addSubview(topLabel)
        topLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(50)
        }
        
        limitLabel.textColor = .init(hex: 0x4392E8)
        limitLabel.font = UIFont.Medium(ofSize: 20)
        limitLabel.text = "0"
        whiteView.addSubview(limitLabel)
        limitLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(topLabel.snp.bottom).offset(20)
        }
        
        let bottomLabel = UILabel()
        bottomLabel.textColor = .black
        bottomLabel.text = "今日已用（张）"
        bottomLabel.font = UIFont.systemFont(ofSize: 16)
        whiteView.addSubview(bottomLabel)
        bottomLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(limitLabel.snp.bottom).offset(50)
        }
        
        todayUseLabel.textColor = .init(hex: 0x4392E8)
        todayUseLabel.font = UIFont.Medium(ofSize: 20)
        todayUseLabel.text = "0"
        whiteView.addSubview(todayUseLabel)
        todayUseLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(bottomLabel.snp.bottom).offset(20)
        }
        
        userBtn.setImage(UIImage(named: "icon_use_paper"), for: .normal)
        userBtn.addTarget(self, action: #selector(userPaperHandle), for: .touchUpInside)
        view.addSubview(userBtn)
        userBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(whiteView.snp.bottom).offset(50)
        }
    }

    
    @objc  func userPaperHandle() {
        if userCount >= limitCount {
            showHUDTextOnly("您今日用纸量已超过限制标准，请节约用纸，保护自然~", duration: 3)
            return
        }
        userCount += 1
        let list = ["\(limitCount)", "\(userCount)"]
        UserDefaults.standard.set(list, forKey: foramtter.string(from: Date()))
        UserDefaults.standard.synchronize()
        limitLabel.text = "\(limitCount)"
        todayUseLabel.text = "\(userCount)"
    }
    
    lazy var foramtter: DateFormatter = {
        let temp = DateFormatter()
        temp.dateFormat = "yyyyMMdd"
        return temp
    }()
    
    func reloadData() {
        if let temp = UserDefaults.standard.object(forKey: foramtter.string(from: Date())) as? [String]{
            limitCount = Int(temp[0]) ?? 0
            userCount = Int(temp[1]) ?? 0
            limitLabel.text = "\(limitCount)"
            todayUseLabel.text = "\(userCount)"
            emptyLabel.isHidden = true
            whiteView.isHidden = false
            userBtn.isHidden = false
        }else{
            emptyLabel.isHidden = false
            whiteView.isHidden = true
            userBtn.isHidden = true
        }
    }

}
